package com.hex.liyue.storemodule.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.LiYueApplication;
import com.hex.liyue.R;
import com.hex.liyue.storemodule.bean.FragmentStoreBean;

import java.util.List;

/**
 * Created by WJ on 2016/6/12.
 */
public class AdapterStore extends RecyclerView.Adapter<AdapterStore.MyHolder> {
    private OnItemClickListener onItemClickListener;

    private List<FragmentStoreBean.DataBean> list;

    public void setList(List<FragmentStoreBean.DataBean> list) {
        this.list = list;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(int position);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_store, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        FragmentStoreBean.DataBean dataBean = list.get(position);
        holder.rl_store.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClickListener(position);
            }
        });
        holder.tv_brand.setText(dataBean.gift_brand);
        holder.tv_name.setText(dataBean.gift_name);
        holder.tv_price.setText("￥" + dataBean.price);
        Glide.with(LiYueApplication.getInstance())
                .load(dataBean.gift_url)
                .error(R.drawable.no_icon)
                .into(holder.iv_avater);
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }


    class MyHolder extends RecyclerView.ViewHolder {
        RelativeLayout rl_store;
        TextView tv_brand, tv_name, tv_price;
        ImageView iv_avater;

        public MyHolder(View itemView) {
            super(itemView);
            rl_store = (RelativeLayout) itemView.findViewById(R.id.rl_store);
            iv_avater = (ImageView) itemView.findViewById(R.id.iv_avater);
            tv_brand = (TextView) itemView.findViewById(R.id.tv_brand);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_price = (TextView) itemView.findViewById(R.id.tv_price);
        }

    }
}
